package com.mzj.saas.commons;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.ssl.SSLContexts;

import javax.net.ssl.SSLContext;
import java.io.InputStream;
import java.security.KeyStore;


public class CertUtil {
	
	/**
     * 加载微信退款证书
     */
    public static SSLConnectionSocketFactory initCert(String mchId, InputStream instream) throws Exception {
 
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(instream, mchId.toCharArray());

        if (null != instream) {
        	instream.close();
        }

        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,mchId.toCharArray()).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

        return sslsf;
    }
}
